package com.l.web.master.login;

import org.apache.log4j.Logger;
import com.l.web.WebConstant;
import com.l.web.util.ControllerSupport;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@Controller
@Scope("prototype")
public class CaptchaController extends ControllerSupport {
    private static final Logger LOGGER = Logger.getLogger(CaptchaController.class);

    @Autowired
    private CaptchaService captchaService;

    @RequestMapping("captcha")
    public void captcha(HttpServletRequest request, HttpServletResponse response, ModelMap modelMap) {
        response.setHeader("Pragma", "no-cache");
        response.setHeader("Cache-Control", "no-cache");
        response.setDateHeader("Expires", 0);
        response.setContentType("image/jpeg");

        try {
            String context = captchaService.draw(response.getOutputStream());
            request.getSession().setAttribute(WebConstant.SESSION_CAPTCHA, context);
        } catch (IOException e) {
            LOGGER.info("验证码图片输出异常：" + e);
        }
    }
}
